//
//  TabModel.m
//  AffectionBee
//
//  Created by 程建良 on 16/8/20.
//  Copyright © 2016年 chivalrous. All rights reserved.
//

#import "TabModel.h"
#import "NetworkTool.h"
#import "ProductModel.h"
#import "DBManager.h"


@implementation TabModel

-(instancetype)initWithDict:(NSDictionary*)dict
{
    TabModel* model = [[TabModel alloc]init];
    model.name = dict[@"name"];

    model.cid = dict[@"id"];


    return model;
}

+(instancetype)tabModelWithDict:(NSDictionary*)dict
{
    return [[TabModel alloc]initWithDict:dict];
}

#pragma mark --获取数据--
+(void)modelListWithUrlStr:(NSString *)urlstr parameters:(NSDictionary *)parameters SuccessBlock:(void (^)(NSArray<TabModel *> *))success failure:(void (^)(NSError *))failure{
    
    
    [[NetworkTool sharedManager]superMarketLoadDataWithUrlStr:urlstr parameters:parameters  SuccessBlock:^(id responseObject) {
        
        
        NSDictionary*dict = responseObject;
        NSDictionary*data = dict[@"data"];
        
        //产品字典
        NSDictionary *products = data[@"products"];
        
        
        
        //分类数组
        NSArray *categories = data[@"categories"];
        
        
        
        //分类数组
        NSMutableArray*listArray = [NSMutableArray array];
        
        //分类模型数组
        NSMutableArray*tempArray = [NSMutableArray array];
        
        
        [categories enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            TabModel *model = [TabModel tabModelWithDict:obj];
            
            //分类模型数组
            [tempArray addObject:model];
            
        }];
        
        
        [tempArray enumerateObjectsUsingBlock:^(TabModel* obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            
            //获取每类的产品数组
            NSString *key = obj.cid;
            //取每一个商品字典数组
            NSArray *productArray = products[key];
            
            
            
            //商品模型数组
            NSMutableArray *tempProArry = [NSMutableArray array];
            
            
            
            
            
            [productArray enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                ProductModel *model = [ProductModel productModelWithDict:obj];
                
                //NSLog(@"%@",model.productId);
                
                //将获取到的网络数据存储到数据库中
                [DBManager AddData:model];
                
                //NSLog(@"%@",NSHomeDirectory());
                
                
                [tempProArry addObject:model];
                
                
            }];
            
            
            
            obj.products = tempProArry.copy;
            
            
            
            [listArray addObject:obj];
        }];
        
        
        success(listArray.copy);
        
        
        
    } failure:failure];
}




-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
}
@end
